{ "cells": [ { "cell_type": "markdown", "id": "80924601", "metadata": {}, "source": [ "# Programming Techniques — Laboratory — INSTRUCTOR VERSION\n", "## Input, Type Conversion and Strings\n", "\n", "This is the **worked version** of the student laboratory.\n", "\n", "It follows the same progression:\n", "1. Guided exercise\n", "2. Semi-independent laboratory exercises\n", "3. Independent review / homework\n", "\n", "Solutions intentionally use only concepts already introduced in the course." ] }, { "cell_type": "markdown", "id": "677b9077", "metadata": {}, "source": [ "---\n", "# Part A — Guided Exercise\n", "## 1. Conference registration receipt\n", "\n", "### Theory recall\n", "\n", "`input()` always returns a **string**:\n", "\n", "```python\n", "name = input(\"Participant name: \")\n", "```\n", "\n", "When a string represents a number, it can be converted:\n", "\n", "```python\n", "tickets = int(\"3\") # 3\n", "price = float(\"12.50\") # 12.5\n", "text = str(42) # \"42\"\n", "```\n", "\n", "### `int()` and number bases\n", "\n", "`int()` can also interpret a string using a specified **base**:\n", "\n", "```python\n", "int(\"1010\", 2) # 10 binary → integer\n", "int(\"1A\", 16) # 26 hexadecimal → integer\n", "int(\"25\", 10) # 25 decimal → integer\n", "\n", "# more explicitly:\n", "int(\"1010\", base=2) # 10 binary → integer\n", "int(\"1A\", base=16) # 26 hexadecimal → integer\n", "int(\"25\", base=10) # 25 decimal → integer\n", "```\n", "\n", "The second argument tells Python how to interpret the digits.\n", "\n", "Invalid digits for the selected base cause a `ValueError`:\n", "\n", "```python\n", "int(\"102\", 2) # ValueError: 2 is not a binary digit\n", "int(\"1G\", 16) # ValueError: G is not a hexadecimal digit\n", "int(\"3.14\") # ValueError: not an integer representation\n", "```\n", "\n", "For decimal numbers with a fractional part, use `float()`:\n", "\n", "```python\n", "float(\"3.14\") # 3.14\n", "```\n", "\n", "### Task\n", "\n", "Build a small registration receipt.\n", "\n", "Ask the user for:\n", "- participant name;\n", "- number of tickets;\n", "- price per ticket;\n", "- a short **binary registration code**.\n", "\n", "Then:\n", "1. convert the numeric inputs;\n", "2. convert the binary code to an integer using `int(..., 2)`;\n", "3. calculate the total cost;\n", "4. print a compact receipt using an **f-string**.\n", "\n", "Example:\n", "\n", "```text\n", "Participant name: Sofia\n", "Number of tickets: 3\n", "Price per ticket: 12.50\n", "Binary registration code: 10101\n", "\n", "Sofia bought 3 tickets.\n", "Total: €37.50\n", "Registration code: 21\n", "```" ] }, { "cell_type": "code", "execution_count": null, "id": "3b456937", "metadata": { "tags": [ "solution" ] }, "outputs": [], "source": [ "name = input(\"Participant name: \")\n", "tickets = int(input(\"Number of tickets: \"))\n", "price = float(input(\"Price per ticket: \"))\n", "binary_code = input(\"Binary registration code: \")\n", "\n", "registration_code = int(binary_code, 2)\n", "total = tickets * price\n", "\n", "print()\n", "print(f\"{name} bought {tickets} tickets.\")\n", "print(f\"Total: €{total:.2f}\")\n", "print(f\"Registration code: {registration_code}\")" ] }, { "cell_type": "markdown", "id": "abc6d2f5", "metadata": {}, "source": [ "### Quick discussion\n", "- What type does `input()` return?\n", "- Why do we need `int()` or `float()` before arithmetic?\n", "- What does the `2` mean in `int(binary_code, 2)`?\n", "- What would happen with `int(\"102\", 2)`?\n", "- What does `{total:.2f}` do inside the f-string?" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python", "version": "3" } }, "nbformat": 4, "nbformat_minor": 5 }